<?PHP

require_once APP_ROOT . "view/common/layout.view.php";
require_once APP_ROOT . "view/common/layout.formview.php";
require_once APP_ROOT . "model/common/player.php";
require_once APP_ROOT . "model/common/team.php";
require_once APP_ROOT . "model/common/league.php";
require_once APP_ROOT . "model/common/event.php";
require_once APP_ROOT . "model/common/article.php";
require_once APP_ROOT . "model/common/subscription.php";
require_once APP_ROOT . "model/common/session.php";
require_once APP_ROOT . "model/common/user.php";
require_once APP_ROOT . "model/common/parameters.php";
require_once APP_ROOT . "model/common/price.php";
require_once APP_ROOT . "model/common/site.php";

class MyprofileView extends LayoutFormView {
	var $option;
	var $site_id;
	var $site_name;

	function MyprofileView($TemplateFile,$option)	{
		parent::LayoutFormView($TemplateFile);
		$this->option=$option;
	}

	function prepare(&$controller, &$request, &$responseModel){
		parent::prepare();

		if(APP_NAME!=""){
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');

		$docbase="http://".$_SERVER['HTTP_HOST'];
		$this->Template->set('docbase',$docbase);

        $this->site_id = Site::getTemplateSiteID();
        $this->site_name = Site::getTitle();

		$statuses=array('Register for free','Paid subscriber','Unregistered','Admin','Article writer','Registered user (former subscriber)');

		if(!Session::isLogin()){
			$status=2;
			$this->forNotlogin();
		}else{
			$user=&Session::getUserArray();

			$numplayerprof=Player::getPlayersCount($user['user_id']);
			$this->Template->set('numplayerprof',$numplayerprof);
			$numteamprof=Team::getTeamsCount($user['user_id']);
			$this->Template->set('numteamprof',$numteamprof);
			$numleagprof=League::getLeaguesCount($user['user_id']);
			$this->Template->set('numleagprof',$numleagprof);
			$numevents=Event::getEventsCount($user['user_id']);
			$this->Template->set('numevents',$numevents);
			$numarticles=Article::getArticlesCount($user['user_id']);
			$this->Template->set('numarticles',$numarticles);
			$numsubscriptions=Subscription::getSubscriptionsCount($user['user_id']);
			$this->Template->set('numsubscriptions',$numsubscriptions);

			$this->Template->set('firstname',$user['firstname']);
			$this->Template->set('lastname',$user['lastname']);

			switch ($this->option){
			case 1:
				$this->forIndex($controller, $request, $responseModel,$user['user_id']);
				break;
			case 2:
				$this->forMessage($controller,$request,$responseModel);
				break;
			case 3:
				$this->forChngpassword($controller,$request,$responseModel,$user['user_id']);
				break;
			case 4:
				$this->forPlayerprofile($controller,$request,$responseModel,$user['user_id']);
				break;
			case 5:
				$this->forTeamprofile($controller,$request,$responseModel,$user['user_id']);
				break;
			case 6:
				$this->forLeagueprofile($controller,$request,$responseModel,$user['user_id']);
				break;
			case 7:
				$this->forEvents($controller,$request,$responseModel,$user['user_id']);
				break;
			case 8:
				$this->forArticles($controller,$request,$responseModel,$user['user_id']);
				break;
			case 9:
				$this->forDeleteitem($controller,$request,$responseModel,$user['user_id']);
				break;
			case 10:
				$this->forSubscriptions($controller,$request,$responseModel,$user['user_id']);
				break;
			}

			//user status
			switch ($user['level'])	{
			case 1:
				$status=4;
				$this->Template->set('artwriter',' ');
				break;
			case 2:
				$status=3;
				$this->Template->set('admin','Admin');
				break;
			case 0:
                $user_id = $user['user_id'];
				if (User::isUserSubscriptionActive($user_id)) $status=1;
				else if (User::isUserSubscriber($user_id)) $status=5;
                else $status=0;

				break;
			}

		}
		$laststatuses='';
		foreach($statuses as $index=>$stat){
			if($index!=$status)
				if($laststatuses!='') $laststatuses.=', '.$stat;
				else $laststatuses.=$stat;
		}
		$this->Template->set('status',$statuses[$status]);
		$this->Template->set('laststatuses',$laststatuses);

	}

	function forSubscriptions(&$controller,&$request,&$responseModel,$user_id){

		$this->Template->set('title',$this->site_name.' - My Profile: Subscription');

		$pager=null;
        //$list =&Subscription::getListForUser($user_id,$pager);
        $list =&Subscription::getListForUser($user_id,$pager);
        $this->Template->setChildDataSource('subscrlist',$list);

        $subscr_id=$responseModel->get('id');
        if ($subscr_id!=null) {
        	$subscr_properties=" ".$responseModel->get('type')." ( ".$responseModel->get('status');
        	if($responseModel->get('reason')!=null) $subscr_properties.=", ".$responseModel->get('reason');
        		$subscr_properties.=" )";
        	$this->Template->set('subscr_properties',$subscr_properties);

			$payments=&Payment::getListForSubscription($subscr_id,$pager);
			$this->Template->setChildDataSource('paylist',$payments);
        }

        $mode = Parameters::getRunningMode();
     	if ($mode) $paypal_url = 'https://www.paypal.com/cgi-bin/webscr';
     	else  $paypal_url = 'http://www.sandbox.paypal.com/cgi-bin/webscr';
     	$responseModel->set('paypal_url',$paypal_url);
     	$this->Template->set('paypal_url',$paypal_url);

        $paypal_email = Parameters::getPaypalEmail();
     	$responseModel->set('business',$paypal_email);
     	$this->Template->set('business',$paypal_email);
	}

	function forDeleteitem(&$controller,&$request,&$responseModel,$user_id){
		$itemtypes=array('player profile','team profile','league/camp profile','calendar event','article');
		$this->Template->set('title',$this->site_name.' - My Profile: Delete item');

		$itemtype=$itemtypes[$request->getParameter('item')];
		$id=$request->getParameter('id');
		$from=getenv("HTTP_REFERER");
		$this->Template->set('item_type',$itemtype);
		$this->Template->set('item_id',$id);
		$this->Template->set('from',$from);

		$el_type=$this->Form->getChild('type');
		$el_type->setValue($request->getParameter('item'));
		$el_id=$this->Form->getChild('id');
		$el_id->setValue($id);
		$el_from=$this->Form->getChild('from');
		$el_from->setValue($from);


	}

	function forEvents(&$controller,&$request,&$responseModel,$user_id){
		$this->Template->set('title',$this->site_name.' - My Profile: Calendar events');
		$responseModel->set('events',true);

		$pager =& $this->Template->getChild('evpagenav');
		$pager2 =& $this->Template->getChild('evpagenav2');


		$list =&Event::getUserEventsList($pager,$user_id);
        $list->paginate($pager2);
		$this->Template->setChildDataSource('evlist',$list);
	}

	function forArticles(&$controller,&$request,&$responseModel,$user_id){
		$this->Template->set('title',$this->site_name.'- My Profile: Articles');
		$responseModel->set('articles',true);
		$this->Template->set('articles',' ');

		Session::loadResponseModel($responseModel,'filter.myarticles');

		$pager =& $this->Template->getChild('artpagenav');
		$pager2 =& $this->Template->getChild('artpagenav2');


		if($responseModel->get('page')==1){
		 $pager->CurrentPage=1;
		 $pager2->CurrentPage=1;
		 $responseModel->set('page',0);
		 Session::saveResponseModel($responseModel,'filter.myarticles');
		}

		if($responseModel->get('filters')!='filtered'){
			$responseModel->set('filters','all');
        	$list =&Article::getUserArticlesList($pager,$user_id);
        	$list->paginate($pager2);
		}else{
			$resent=$responseModel->get('resent');
			$list =&Article::getUserArticlesList($pager,$user_id,$resent);
        	$list->paginate($pager2);
		}
        $this->Template->setChildDataSource('artlist',$list);

        $resents =& $this->Form->getChild('resent');
     	$list_resent =Article::getResentList();
        $resents->setChoices($list_resent);
  		$resent = $responseModel->get('resent');
        $resents->setSelection($resent);
	}

	function forLeagueprofile(&$controller,&$request,&$responseModel,$user_id){
		$this->Template->set('title',$this->site_name.' - My Profile: League/camp profiles');
		$responseModel->set('leagueprofile',true);
		$this->Template->set('leagueprofile',' ');

		Session::loadResponseModel($responseModel,'filter.myleagues');

		$pager =& $this->Template->getChild('leagpagenav');
		$pager2 =& $this->Template->getChild('leagpagenav2');

		if($responseModel->get('page')==1){
		 $pager->CurrentPage=1;
		 $pager2->CurrentPage=1;
		 $responseModel->set('page',0);
		 Session::saveResponseModel($responseModel,'filter.myleagues');
		}

		if($responseModel->get('filters')!='filtered'){
			$responseModel->set('filters','all');
        	$list =&League::getUserLeaguesList($pager,$user_id);
        	$list->paginate($pager2);
		}else{
			$league=$responseModel->get('event');
			$list =&League::getUserLeaguesList($pager,$user_id,$league);
        	$list->paginate($pager2);
		}
        $this->Template->setChildDataSource('leaglist',$list);

        $events =& $this->Form->getChild('event');
     	$list_event =League::getEventsList();
        $events->setChoices($list_event);
  		$event = $responseModel->get('event');
        $events->setSelection($event);
	}

	function forTeamprofile(&$controller,&$request,&$responseModel,$user_id){
		$this->Template->set('title',$this->site_name.' - My Profile: Team profiles');
		$responseModel->set('teamprofile',true);
		$this->Template->set('teamprofile',' ');

		Session::loadResponseModel($responseModel,'filter.myteams');

		$pager =& $this->Template->getChild('teampagenav');
		$pager2 =& $this->Template->getChild('teampagenav2');

		if($responseModel->get('page')==1){
		 $pager->CurrentPage=1;
		 $pager2->CurrentPage=1;
		 $responseModel->set('page',0);
		 Session::saveResponseModel($responseModel,'filter.myteams');
		}

		if($responseModel->get('filters')!='filtered'){
			$responseModel->set('filters','all');
        	$list =&Team::getUserTeamsList($pager,$user_id);
        	$list->paginate($pager2);
		}else{
			$league=$responseModel->get('league');
			$list =&Team::getUserTeamsList($pager,$user_id,$league);
        	$list->paginate($pager2);
		}
        $this->Template->setChildDataSource('teamlist',$list);

        $leagues =& $this->Form->getChild('league');
     	$list_league =Team::getLeaguesList();
        $leagues->setChoices($list_league);
  		$league = $responseModel->get('league');
        $leagues->setSelection($league);
	}


	function forPlayerprofile(&$controller,&$request,&$responseModel,$user_id){
		$this->Template->set('title',$this->site_name.' - My Profile: Player profiles');
		$responseModel->set('playerprofile',true);
		$this->Template->set('playerprofile',' ');

		Session::loadResponseModel($responseModel,'filter.myplayers');

		$pager =& $this->Template->getChild('pagenav');
		$pager2 =& $this->Template->getChild('pagenav2');

		if($responseModel->get('page')==1){
		 $pager->CurrentPage=1;
		 $pager2->CurrentPage=1;
		 $responseModel->set('page',0);
		 Session::saveResponseModel($responseModel,'filter.myplayers');
		}

		if($responseModel->get('filters')!='filtered'){
			$responseModel->set('filters','all');
        	$list =&Player::getUserPlayesList($pager,$user_id);
        	$list->paginate($pager2);
		}else{
			$class=$responseModel->get('class');
			$list =&Player::getUserPlayesList($pager,$user_id,$class);
        	$list->paginate($pager2);
		}
        $this->Template->setChildDataSource('pllist',$list);

        $classes =& $this->Form->getChild('class');
     	$list =&Player::getClassesList();
        $classes->setChoices($list);
  		$class = $responseModel->get('class');
        $classes->setSelection($class);

	}


	function forNotlogin(){
		$this->Template->set('title',$this->site_name.' - My Profile');

	}


	function forMessage(&$controller, &$request, &$responseModel){
		$this->Template->set('title',$this->site_name.' - My Profile');

		$message=$responseModel->get('message');
		error_log("[message2]:".$message);
		$this->Template->set('message',$message);
	}


	function forChngpassword(&$controller, &$request, &$responseModel,$id){
		$responseModel->set('chngpassword',true);
		$this->Template->set('title',$this->site_name.' - My Profile: Change password');
		$this->Template->set('chnfpassword',' ');
	}


	function forIndex(&$controller, &$request, &$responseModel,$id){
		$responseModel->set('index',true);
		//$fieldsArray=array('firstname','lastname','email','age','home_phone','work_phone',
		//					'address1','address2','city','country','zip','login');
        $this->Template->set('title',$this->site_name.' - My Profile'); 
		$states =& $this->Template->getChild('state');
     	$states_list =& State::getStates();
        $states->setChoices($states_list);

		$this->Template->set('title',$this->site_name.' - My Profile');
		$this->Template->set('index',' ');
		$user =& User::getRecord($id);

		if(!$responseModel->get('retrieve')){
			if ($user) $responseModel->merge($user->export());
			if(!$user->get('age')||$user->get('age')==0){
				$ageel=$this->Form->getChild('age');
				$ageel->setValue(' ');
			}
		}

		if($responseModel->get('exlogin'))
			$this->Template->set('exlogin',true);
		//$properties=$user->getPropertyList();
		//foreach ($properties as $propindex=>$property){
			//if(in_array($property,$fieldsArray)){
				//$value = $user->get($property);
				//if ( $this->Form->hasChild($property) )   {
					//$field =& $this->Form->getChild($property);
					//$field->setValue($value);
				//}
			//}
		//}

  		//$state = $user->get('state');
        //$states->setSelection($state);
	}
}


?>
